package com.mjt.dongbao.common.base.exceptionHandle;

import com.baomidou.kaptcha.exception.KaptchaException;
import com.baomidou.kaptcha.exception.KaptchaIncorrectException;
import com.baomidou.kaptcha.exception.KaptchaNotFoundException;
import com.baomidou.kaptcha.exception.KaptchaTimeoutException;
import com.mjt.dongbao.common.base.result.ResultWrapper;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Description
 * @Author mjt
 * @Time 2022/1/20
 */
@ResponseBody
@ControllerAdvice
public class CaptchaVerifyHandler {

    // 验证码校验不通过异常处理
    @ExceptionHandler(KaptchaException.class)
    public ResultWrapper kcaptchaException(KaptchaException e){
        if (e instanceof KaptchaTimeoutException){
            return ResultWrapper.builder().code(1004).msg("超时").build();
        }else if (e instanceof KaptchaIncorrectException){
            return ResultWrapper.builder().code(1004).msg(e.getMessage()).build();
        }else if (e instanceof KaptchaNotFoundException){
            return ResultWrapper.builder().code(1004).msg(e.getMessage()).build();
        }else {
            return ResultWrapper.builder().code(1003).msg("反正错了").build();
        }
    }

}
